/**
 * Created by yujia on 2017/11/18.
 */
var tag = false; //回答正确or错误
var result; //答题记录
var question = { //题目
    id: null,
    totalNumber: 0,
    currentNumber: 0,
    classify: null,
    type: null,
    title: null,
    answer: {
        a: null,
        b: null,
        c: null,
        d: null
    },
    correctAnswer: null
}; //题目

$(document).ready(function () {
    // $("div.well").hide();
    // clearResult();
    init();
});

$("#next").click(function () {
    // 只有选择了答案才能进入下一题
    if (!$("#answer>button").hasClass("btn-primary")) {
        alert("请选择答案");
    } else if (question.currentNumber < question.totalNumber - 1) {
        //记录用户答案
        submitResult();
        //获取下一题
        question.currentNumber++;
        askQuestion();
    } else {
        //记录用户答案
        submitResult();
        $(location).attr('href', '/views/result.html');
    }
});

// $("#pre").click(function () {
//     $("div.well").hide();
//     if (question.currentNumber > 0) {
//         question.currentNumber--;
//         readResult();
//     } else {
//         alert("已经是第1题");
//     }
// });

$("body").on("click", "#answer>button", function () {
    $("#answer>button").addClass("btn-default");
    $("#answer>button").removeClass("btn-primary");
    $(this).addClass("btn-primary");
    // this.addClass("btn-primary"); //报错,this代表window
    var correct = question.correctAnswer;
    var tip = $(this).attr("tip");
    tag = false; //true就是答对了
    if ($.trim(correct) == $.trim(tip)) {
        tag = true;
    }
    if (tag == true) { //答题正确,不用显示答案提示
        $("div.well").html()
        $("div.well:visible").hide();
    }
    if (tag == false) { //答题错误,显示答案提示
        var str;
        $("#answer>button").each(function () {
            if ($(this).attr("tip") == question.correctAnswer) {
                str = "正确答案:&nbsp;&nbsp;&nbsp;" + $(this).text();
            }
        });
        $("#result").addClass("label label-danger");
        $("#result").html(str);
        $("div.well:hidden").show();
    }

});

function init() {
    $("#classify").html("<h1>" + $.cookie("classify") + "</h1>");
    // clearResult();
    askQuestion();
}

function askQuestion() {
    $("div.well").hide();
    tag = false;
    $.ajax({
        type: 'POST',
        url: "/exam/question/current.json",
        contentType: "application/json;",
        data: JSON.stringify({"classify": $.cookie("classify"), "currentNumber": question.currentNumber}),
        success: function (data) {
            question.id = data.id,
                question.currentNumber = data.currentNumber;
            question.totalNumber = data.totalNumber;
            question.classify = data.classify;
            question.type = data.type;
            question.title = data.title;
            question.answer.a = data.answers[0];
            question.answer.b = data.answers[1];
            question.answer.c = data.answers[2];
            question.answer.d = data.answers[3];
            question.correctAnswer = data.correctAnswer;

            var currentPage = question.currentNumber + 1;
            var totalPage = question.totalNumber;
            $("#classify").html("<h1>" + $.cookie("classify")
                + " ( " + currentPage + "/" + totalPage + " ) "
                + "</h1>");

            $("#title").html("<h4>" + question.title + "</h4>");
            var strAnswer = "";
            if (question.type == "judge") {
                strAnswer += '<button type="button" class="btn btn-default btn-lg btn-block" tip="1">  正确 </button>'
                strAnswer += '<button type="button" class="btn btn-default btn-lg btn-block" tip="0">  错误 </button>'
            }
            if (question.type == "choice") {
                strAnswer += '<button type="button" class="btn btn-default btn-lg btn-block" tip="A">' + question.answer.a + '</button>'
                strAnswer += '<button type="button" class="btn btn-default btn-lg btn-block" tip="B">' + question.answer.b + '</button>'
                strAnswer += '<button type="button" class="btn btn-default btn-lg btn-block" tip="C">' + question.answer.c + '</button>'
                strAnswer += '<button type="button" class="btn btn-default btn-lg btn-block" tip="D">' + question.answer.d + '</button>'
            }
            // $("#answer").attr("correctanswer",data.correctAnswer);
            $("#answer").html(strAnswer);
        }, error: function (xhr, type) {
            $(location).attr('href', '/views/result.html');
        }
    });
}

function submitResult() {
    var result = {
        questionId: question.id,
        classify: question.classify,
        userId: 123, //@TODO
        userAnswer: $("#answer>button.btn-primary").attr("tip"),
        isRight: Number(tag)
    };

    $.ajax({
        type: 'POST',
        url: "/result/submit.json",
        contentType: "application/json;",
        data: JSON.stringify(result),
        success: function () {
        }, error: function (xhr, type) {
            $(location).attr('href', '/views/result.html');
        }
    });
}

function clearResult() {
    $.ajax({
        type: 'POST',
        url: "/result/clear.json",
        contentType: "application/json;",
        data: JSON.stringify({classify:question.classify}),
        success: function () {
        }, error: function (xhr, type) {
            $(location).attr('href', '/views/result.html');
        }
    });
}


// function readResult() {
//     // question.currentNumber - 1;
//     $.ajax({
//         type: 'POST',
//         url: "/result/read.json",
//         contentType: "application/json;",
//         data: JSON.stringify({id :"1" }),
//         success: function (data) {
//
//         }, error: function (xhr, type) {
//             //TODO 转到错误页面
//         }
//     });
// }